package com.zzj.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.base.Result;
import com.zzj.entity.Patient;
import com.zzj.entity.PatientAppointment;
import com.zzj.model.dto.QueryListDTO;
import com.zzj.model.dto.patient.PatientCreateDTO;
import com.zzj.model.dto.patient.PatientUpdateDTO;
import com.zzj.service.IPatientService;
import com.zzj.service.IUserService;
import com.zzj.utils.JWTUtil;
import com.zzj.utils.QueryUtil;
import com.zzj.utils.Utils;
import jakarta.annotation.Resource;
import org.apache.http.HttpHeaders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/patient")
public class PatientController {

    @Resource
    private IPatientService patientService;
    @Resource
    private IUserService userService;

    @Transactional
    @PostMapping("create")
    public Result create(@RequestBody @Validated PatientCreateDTO in) {
        Patient patient = JSONObject.parseObject(JSONObject.toJSONString(in), Patient.class);
        String patientNum = Utils.getPatientNum();
        patient.setPatientNum(patientNum);
        boolean save = patientService.save(patient);
        boolean update = userService.setUserStatus(in.getUsername(), 2);
        if (save && update) {
            return Result.success("患者档案建立成功");
        }
        return Result.error("患者档案建立失败");
    }

    @PostMapping("update")
    public Result update(@RequestBody @Validated PatientUpdateDTO patientUpdateDTO) {
        UpdateWrapper<Patient> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", patientUpdateDTO.getUsername())
                .set("sex", patientUpdateDTO.getSex())
                .set("birthday", patientUpdateDTO.getBirthday())
                .set("culture", patientUpdateDTO.getCulture())
                .set("phone", patientUpdateDTO.getPhone())
                .set("region", patientUpdateDTO.getRegion())
                .set("address", patientUpdateDTO.getAddress());
        boolean update = patientService.update(updateWrapper);
        if (update) {
            return Result.success("信息修改成功");
        } else {
            return Result.error("信息修改失败");
        }
    }

    @PostMapping("info")
    public Result info(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String token) {
        String username = JWTUtil.getUsername(token);

        Patient one = patientService.getOne(Wrappers.<Patient>lambdaQuery()
                        .eq(Patient::getUsername, username),
                false);
        return Result.success(one);
    }

    @PostMapping("list")
    public Result list(@RequestBody @Validated QueryListDTO in) {

        QueryWrapper<Patient> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        Page<Patient> patientPage = new Page<>(in.getPageNo(), in.getPageSize());
        IPage<Patient> patientIPage = patientService.getBaseMapper().selectPage(patientPage, queryWrapper);
        return Result.success(patientIPage);
    }

}
